$(function() {
	var teamGrid = $("#teamGrid");
	// 初始化
	teamGrid.datagrid({
		url : 'team/list',
		fit : true,
		border : false,
		rownumbers : true,
		pagination : true, // 是否显示分页栏
		columns : [ [
				{
					field : 'team_name',
					title : '团队名称',
					align : 'center',
					width : 160
				},{
					field : 'user_name',
					title : '团队创建人姓名',
					align : 'center',
					width : 100
				},
				{
					field : 'teamCount',
					title : '团队人数',
					align : 'center',
					width : 100
				},{
					field : 'class_name',
					title : '团队所属班级',
					align : 'center',
					width : 100
				},{
					field : 'shenfen',
					title : '组长姓名',
					align : 'center',
					width : 80,
					formatter : function(value, row, index) {
						if (value==null) {
							return "暂无";
						}else{
							return value;
						}
					}
				},
				{
					field : 'team_create_time',
					title : '团队创建时间',
					sortable : true,
					align : 'center',
					width : 120,
					formatter : function(value, row, index) {
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 100,
					formatter : function(value, row, index) {
						var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr delete">删除</a>');
						return bts.join('');
					}
				} ] ],
		toolbar : [ {
			iconCls : 'icon-add',
			text : '添加',
			handler : function() {
				addTeam();
			}
		} ]
	});
	var teamName = $("#teamName");
	var classId=$("#classId")
	$("#teamSearch").on('click', function() {
		teamGrid.datagrid("load", {
			teamName : "%" + teamName.val() + "%",
			projectId :classId.val()
		});
	});
	var teamSearchForm = $("#teamSearchForm")
	$("#teamReset").on('click', function() {
		teamSearchForm.form("clear");
		teamGrid.datagrid("load", {});
	});
	
	var gridPanel = teamGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = teamGrid.datagrid("getRows");
		var id = rows[index].team_id;
		addTeam(id);
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var rows = teamGrid.datagrid("getRows");
		var id = rows[index].team_id;
		$.messager.confirm("提示", "你确定要删除该团队？", function(r) {
			if (r) {
				$.get("team/delete/" + id, function(rs) {
				//	if (rs.success) {
						teamGrid.datagrid("reload");
				/*	} else {
						$.message.alert("提示", rs.message);
					}*/
				});
			}
		});
	});
	function addTeam(id) {
		var dialog = $("<div/>").dialog({
			href : 'jiaoxue/jx-add-team' + (id ? ("/" + id) : ""),
			title : "团队",
			height : 520,
			width : 500,
			modal : true,
			onClose : function() {
				// 窗口关闭事销毁此窗口
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var addTeamForm = $("#addTeamForm");
					if (addTeamForm.form('validate')) {
						$.post("team/save", addTeamForm.serialize(), function(rs) {
							if (rs.success) {
								teamGrid.datagrid("reload");
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}
				}
			}, {
				iconCls : 'icon-clear',
				text : '取消',
				handler : function() {
					dialog.dialog("close");
				}
			} ]
		});
	}
	
});